package com.realife.mbplus.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.alpha.centity.gulisuper.Brand;
import com.realife.mbplus.mapper.BrandMapper;
import com.realife.mbplus.service.BrandService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @version V1.0
 * @author: Albert
 * @date: 2020/5/26 21:50
 * @description:
 */
@Slf4j
@Service(version = "1.1")
@Component("brandService11")
@Transactional
public class BrandServiceImpl2 implements BrandService {

    @Autowired
    private BrandMapper brandMapper;

    @Override
    public List<Brand> queryAllBrand() {

        try {
            TimeUnit.SECONDS.sleep(1);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        List<Brand> brands = this.brandMapper.selectList(null);
        return brands;
    }

    @Override
    public String updateById(Long id) {

        Brand brand = this.brandMapper.selectById(id);
        brand.setName("HHH");

//        Brand brand = new Brand();
//        brand.setId(id);
//        brand.setName("HTCCC");

        int count = this.brandMapper.updateById(brand);
        if (1 != count) {
            throw new RuntimeException("data modify error");
        }

        return "数据修改成功...";
    }

    private static Lock lock = new ReentrantLock();
    @Override
    public String insertBrand() {

        lock.lock();

        Brand brand = Brand.builder()
                .image("xxx")
                .letter('X')
                .name("xxxxxx")
                .build();

        int insert = this.brandMapper.insert(brand);
        if (1 != insert) {
            throw new RuntimeException("insert data error...");
        }

        lock.unlock();

        return "插入数据成功... version =1.1";
    }

}

